{*
* 2007-2014 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to license@prestashop.com so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author PrestaShop SA <contact@prestashop.com>
*  @copyright  2007-2014 PrestaShop SA
*  @license    http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
*}

{*{assign "url" $smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI}*}

<script>
    var baseDir = '{$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI}';
    //   var baseDir='123';
//var baseDir ="/admin4966"
    var baseDir = "";
    $(function() {
        $(".add_container").click(function() {
            $(this).prop("disabled", true);
            var str = this.id;
            var res = str.substr(24);

            var container = $('#container_' + res).val();
            var container_list = $('#container-list-container_' + res).val();

            if ($('#shelf_' + res).val().length == 0) {
                alert('Please input the shelf');
                $('#shelf-list-shelf_' + res).focus();
                $(this).prop("disabled", false);
                return false;
            } else {
                var shelf = $('#shelf_' + res).val();
                var shelf_list = $('#shelf-list-shelf_' + res).val();
            }
            if ($('#bin-list-bin_' + res).val() == -1 && $('#bin_' + res).val().length == 0) {
                alert('Please input the bin');
                $(this).prop("disabled", false);
                return false;
            }
            if ($('#bin_' + res).val().length == 0 && $('#container_' + res).val().length > 0) {
                alert('Please input the bin');
                $('#bin-list-bin_' + res).focus();
                $(this).prop("disabled", false);
                return false;
            } else {
                var bin = $('#bin_' + res).val();
                var bin_list = $('#bin-list-bin_' + res).val();
            }
            var query = $.ajax({
                type: 'POST',
                url: baseDir + '\myajax.php',
                data: {
                    'method': 'addContainer',
                    'id_warehouse': res,
                    'id_shelf': shelf,
                    'shelf_list': shelf_list,
                    'id_bin': bin,
                    'bin_list': bin_list,
                    'id_container': container,
                    'container_list': container_list,
                },
                dataType: 'json',
                success: function(json) {
                    if (json.status == true) {
                        //  alert(json.message)
                    } else {
                        alert(json.error)
                    }
                    $(this).prop("disabled", false);
                    location.reload(true);
                }
            });

        });
        $(".del").click(function() {
            var str = this.id;
            var res = str.split("-");
            //shelf

            if (res[1] == "shelf") {
                //bin
                var result = confirm("Want to delete shelf " + $(this).data('name') + "?");
                if (result == false) {
                    return false;
                }
                deleteShelf(res[2]);
            } else if (res[1] == "bin") {
                //container
                var result = confirm("Want to delete bin " + $(this).data('name') + "?");
                if (result == false) {
                    return false;
                }
                deleteBin(res[2]);
            } else if (res[1] == "container") {
                var result = confirm("Want to delete container " + $(this).data('name') + "?");
                if (result == false) {
                    return false;
                }
                deleteContainer(res[2]);
            }
        });
        $(".shelf-list").change(function() {
            var value = $(this).val();
            var res = this.id.split("-");
            var id = res[2];
            if (value == -1 || value == 0) {
                $('#' + id).val('');
            } else {
                $('#' + id).val(value);
            }
            if (value == -1) {
                $('#' + id).show();
            } else if (value == 0) {
                $('#' + id).hide();
            } else {
                $('#' + id).hide();
            }

            var id_warehouse = id.replace('shelf_', '')
            showBinList(id_warehouse, value);
            showContainerList(id_warehouse);
            $('#bin-list-bin_' + id_warehouse).trigger('change');
            $('#container-list-container_' + id_warehouse).trigger('change');
        });
        $(".bin-list").change(function() {
            var value = $(this).val();
            var res = this.id.split("-");
            var id = res[2];
            if (value == -1 || value == 0) {
                $('#' + id).val('');
            } else {
                $('#' + id).val(value);
            }
            if (value == -1) {
                $('#' + id).show();
            } else if (value == 0) {
                $('#' + id).hide();
            } else {
                $('#' + id).hide();
            }
            var id_warehouse = id.replace('bin_', '')
            showContainerList(id_warehouse, value);
            $('#container-list-container_' + id_warehouse).trigger('change');
        });
        $(".container-list").change(function() {
            var value = $(this).val();
            var res = this.id.split("-");
            var id = res[2];
            if (value == -1 || value == 0) {
                $('#' + id).val('');
            } else {
                $('#' + id).val(value);
            }
            if (value == -1) {
                $('#' + id).show();
            } else if (value == 0) {
                $('#' + id).hide();
            } else {
                $('#' + id).hide();
            }
        });
        $('.shelf-list option[value="0"]').attr("selected", true);
        $('.shelf-list').trigger('change');
        $('.add_container').prop("disabled", false);
    });
    function showBinList(id_warehouse, id_shelf) {

        var query = $.ajax({
            type: 'POST',
            url: baseDir + '\myajax.php',
            data: {
                'method': 'getBinList',
                'id_shelf': id_shelf,
            },
            dataType: 'json',
            success: function(json) {
                $('#bin-list-bin_' + id_warehouse).empty();
                $('#bin-list-bin_' + id_warehouse).append($('<option>').text('').attr('value', 0));
                $('#bin-list-bin_' + id_warehouse).append($('<option>').text('New Bin').attr('value', -1));
                $.each(json, function(i, value) {
                    $('#bin-list-bin_' + id_warehouse).append($('<option>').text(value.name).attr('value', value.id_bin));
                });

            }
        });
    }
    function showContainerList(id_warehouse, id_bin) {
        var query = $.ajax({
            type: 'POST',
            url: baseDir + '\myajax.php',
            data: {
                'method': 'getContainerList',
                'id_bin': id_bin,
            },
            dataType: 'json',
            success: function(json) {
                $('#container-list-container_' + id_warehouse).empty();
                $('#container-list-container_' + id_warehouse).append($('<option>').text('').attr('value', 0));
                $('#container-list-container_' + id_warehouse).append($('<option>').text('New Container').attr('value', -1));
                $.each(json, function(i, value) {
                    $('#container-list-container_' + id_warehouse).append($('<option>').text(value.name).attr('value', value.id_container));
                });

            }
        });
    }
    function deleteShelf(id_shelf) {
        var query = $.ajax({
            type: 'POST',
            url: baseDir + '\myajax.php',
            data: {
                'method': 'deleteShelf',
                'id_shelf': id_shelf,
            },
            dataType: 'json',
            success: function(json) {
                if (json.status == false) {
                    alert(json.error)
                }
                location.reload(true);
            }
        });
    }
    function deleteBin(id_bin) {
        var query = $.ajax({
            type: 'POST',
            url: baseDir + '\myajax.php',
            data: {
                'method': 'deleteBin',
                'id_bin': id_bin,
            },
            dataType: 'json',
            success: function(json) {
                if (json.status == false) {
                    alert(json.error)
                }
                location.reload(true);
            }
        });
    }
    function deleteContainer(id_container) {
        var query = $.ajax({
            type: 'POST',
            url: baseDir + '\myajax.php',
            data: {
                'method': 'deleteContainer',
                'id_container': id_container,
            },
            dataType: 'json',
            success: function(json) {
                if (json.status == false) {
                    alert(json.error)
                }
                location.reload(true);
            }
        });
    }

</script>
<tbody>
    {* {$smarty.const._PS_ADMIN_DIR_|@var_dump}*}
    {assign "warehouse" "0"}
    {if count($list)}
        {foreach $list AS $index => $tr}
            {if  $tr.id_warehouse!=$warehouse}
                {*    {if $warehouse>0}*}
                {include file="$path/themes/default/template/helpers/list/list_content_warehouse_addfield.tpl"}
                {*    {/if}*}
                {assign "warehouse" $tr.id_warehouse}
            {/if}
            <tr
                {if $position_identifier}id="tr_{$position_group_identifier}_{$tr.$identifier}_{if isset($tr.position['position'])}{$tr.position['position']}{else}0{/if}"{/if}
                class="{if isset($tr.class)} {$tr.class}{/if} {if $tr@iteration is odd by 1}odd{/if}"
                {if isset($tr.color) && $color_on_bg}style="background-color: {$tr.color}"{/if} >
                {if $bulk_actions && $has_bulk_actions}
                    <td class="text-center">
                        {if isset($list_skip_actions.delete)}
                            {if !in_array($tr.$identifier, $list_skip_actions.delete)}
                                <input type="checkbox" name="{$list_id}Box[]" value="{$tr.$identifier}"{if isset($checked_boxes) && is_array($checked_boxes) && in_array({$tr.$identifier}, $checked_boxes)} checked="checked"{/if} class="noborder" />
                            {/if}
                        {else}
                            <input type="checkbox" name="{$list_id}Box[]" value="{$tr.$identifier}"{if isset($checked_boxes) && is_array($checked_boxes) && in_array({$tr.$identifier}, $checked_boxes)} checked="checked"{/if} class="noborder" />
                        {/if}
                    </td>
                {/if}
                {foreach $fields_display AS $key => $params}
                    {block name="open_td"}
                        <td
                            {if isset($params.position)}
                                id="td_{if !empty($position_group_identifier)}{$position_group_identifier}{else}0{/if}_{$tr.$identifier}"
                            {/if}
                            class="{if !$no_link}pointer{/if}
                            {if isset($params.position) && $order_by == 'position'  && $order_way != 'DESC'} dragHandle{/if}
                            {if isset($params.class)} {$params.class}{/if}
                            {if isset($params.align)} {$params.align}{/if}"
                            {if (!isset($params.position) && !$no_link && !isset($params.remove_onclick))}
                                onclick="document.location = '{$current_index}&{$identifier}={$tr.$identifier}{if $view}&view{else}&update{/if}{$table}&token={$token}'">
                            {else}
                                >
                            {/if}
                        {/block}
                        {block name="td_content"}
                        {if isset($params.prefix)}{$params.prefix}{/if}

                        {if isset($params.badge_success) && $params.badge_success && isset($tr.badge_success) && $tr.badge_success == $params.badge_success}<span class="badge badge-success">{/if}
                            {if isset($params.badge_warning) && $params.badge_warning && isset($tr.badge_warning) && $tr.badge_warning == $params.badge_warning}<span class="badge badge-warning">{/if}
                                {if isset($params.badge_danger) && $params.badge_danger && isset($tr.badge_danger) && $tr.badge_danger == $params.badge_danger}<span class="badge badge-danger">{/if}
                                    {if isset($params.color) && isset($tr[$params.color])}
                                        <span class="label color_field" style="background-color:{$tr[$params.color]};color:{if Tools::getBrightness($tr[$params.color]) < 128}white{else}#383838{/if}">
                                        {/if}
                                        {if isset($tr.$key)}
                                            {if isset($params.active)}
                                                {$tr.$key}
                                            {elseif isset($params.activeVisu)}
                                                {if $tr.$key}
                                                    <i class="icon-check-ok"></i> {l s='Enabled'}
                                                {else}
                                                    <i class="icon-remove"></i> {l s='Disabled'}
                                                {/if}

                                            {elseif isset($params.position)}
                                                {if $order_by == 'position' && $order_way != 'DESC'}
                                                    <div class="dragGroup">
                                                        <div class="positions">
                                                            {$tr.$key.position}
                                                        </div>
                                                    </div>
                                                {else}
                                                    {$tr.$key.position + 1}
                                                {/if}
                                            {elseif isset($params.image)}
                                                {$tr.$key}
                                            {elseif isset($params.icon)}
                                                {if is_array($tr[$key])}
                                                    {if isset($tr[$key]['class'])}
                                                        <i class="{$tr[$key]['class']}"></i>
                                                    {else}
                                                        <img src="../img/admin/{$tr[$key]['src']}" alt="{$tr[$key]['alt']}" title="{$tr[$key]['alt']}" />
                                                    {/if}
                                                {else}
                                                    <i class="{$tr[$key]}"></i>
                                                {/if}
                                            {elseif isset($params.type) && $params.type == 'price'}
                                                {displayPrice price=$tr.$key}
                                            {elseif isset($params.float)}
                                                {$tr.$key}
                                            {elseif isset($params.type) && $params.type == 'date'}
                                                {dateFormat date=$tr.$key full=0}
                                            {elseif isset($params.type) && $params.type == 'datetime'}
                                                {dateFormat date=$tr.$key full=1}
                                            {elseif isset($params.type) && $params.type == 'decimal'}
                                                {$tr.$key|string_format:"%.2f"}
                                            {elseif isset($params.type) && $params.type == 'percent'}
                                                {$tr.$key} {l s='%'}
                                                {* If type is 'editable', an input is created *}
                                            {elseif isset($params.type) && $params.type == 'editable' && isset($tr.id)}
                                                <input type="text" name="{$key}_{$tr.id}" value="{$tr.$key|escape:'html':'UTF-8'}" class="{$key}" />
                                            {elseif isset($params.callback)}
                                                {if isset($params.maxlength) && Tools::strlen($tr.$key) > $params.maxlength}
                                                    <span title="{$tr.$key}">{$tr.$key|truncate:$params.maxlength:'...'}</span>
                                                {else}
                                                    {$tr.$key}
                                                {/if}
                                            {elseif $key == 'color'}
                                                {if !is_array($tr.$key)}
                                                    <div style="background-color: {$tr.$key};" class="attributes-color-container"></div>
                                                {else} {*TEXTURE*}
                                                    <img src="{$tr.$key.texture}" alt="{$tr.name}" class="attributes-color-container" />
                                                {/if}
                                            {elseif isset($params.maxlength) && Tools::strlen($tr.$key) > $params.maxlength}
                                                <span title="{$tr.$key|escape:'html':'UTF-8'}">{$tr.$key|truncate:$params.maxlength:'...'|escape:'html':'UTF-8'}</span>
                                            {elseif $key=="shelf"||$key=="bin"||$key=="container"}
                                                {$tr.$key|escape:'html':'UTF-8'}

                                            {/if}
                                        {else}
                                            {block name="default_field"}--{/block}
                                        {/if}
                                    {if isset($params.suffix)}{$params.suffix}{/if}
                                    {if isset($params.color) && isset($tr.color)}
                                    </span>
                                {/if}
                                {if isset($params.badge_danger) && $params.badge_danger && isset($tr.badge_danger) && $tr.badge_danger == $params.badge_danger}</span>{/if}
                            {if isset($params.badge_warning) && $params.badge_warning && isset($tr.badge_warning) && $tr.badge_warning == $params.badge_warning}</span>{/if}
                        {if isset($params.badge_success) && $params.badge_success && isset($tr.badge_success) && $tr.badge_success == $params.badge_success}</span>{/if}
                        {if $key=="shelf" && $tr.id_shelf neq ''}
                        <a href="#" id ="del-{$key}-{$tr.id_shelf}" class="del" data-name="{$tr.$key}">Del</a>
                    {elseif $key=="bin"&& $tr.id_bin neq ''}
                        <a href="#" id ="del-{$key}-{$tr.id_bin}" class="del" data-name="{$tr.$key}">Del</a>
                    {elseif $key=="container" && $tr.id_container neq ''}
                        <a href="#" id ="del-{$key}-{$tr.id_container}" class="del" data-name="{$tr.$key}">Del</a>
                    {/if}
                {/block}
                {block name="close_td"}
                </td>
            {/block}
        {/foreach}

        {if $shop_link_type}
            <td title="{$tr.shop_name}">
                {if isset($tr.shop_short_name)}
                    {$tr.shop_short_name}
                {else}
                    {$tr.shop_name}
                {/if}
            </td>
        {/if}
        {if $has_actions}
            <td class="text-right">
                {assign var='compiled_actions' value=array()}
                {foreach $actions AS $key => $action}
                    {if isset($tr.$action)}
                        {if $key == 0}
                            {assign var='action' value=$action}
                        {/if}
                        {if $action == 'delete' && $actions|@count > 2}
                            {$compiled_actions[] = 'divider'}
                        {/if}
                        {$compiled_actions[] = $tr.$action}
                    {/if}
                {/foreach}
                {if $compiled_actions|count > 0}
                    {if $compiled_actions|count > 1}<div class="btn-group-action">{/if}
                        <div class="btn-group pull-right">
                            {$compiled_actions[0]|regex_replace:'/class\s*=\s*"(\w*)"/':'class="$1 btn btn-default"'}
                            {if $compiled_actions|count > 1}
                                <button class="btn btn-default dropdown-toggle" data-toggle="dropdown">
                                    <i class="icon-caret-down"></i>&nbsp;
                                </button>
                                <ul class="dropdown-menu">
                                    {foreach $compiled_actions AS $key => $action}
                                        {if $key != 0}
                                            <li {if $action == 'divider'}class="divider"{/if}>
                                            {if $action != 'divider'}{$action}{/if}
                                        </li>
                                    {/if}
                                {/foreach}
                            </ul>
                        {/if}
                    </div>
                    {if $compiled_actions|count > 1}</div>{/if}
                {/if}
        </td>
    {/if}
</tr>


{/foreach}
{else}
    <tr>
        <td class="list-empty" colspan="{count($fields_display) + 2}">
            <div class="list-empty-msg">
                <i class="icon-warning-sign list-empty-icon"></i>
                {l s='No records found'}
            </div>
        </td>
    </tr>
{/if}
</tbody>
